package com.jinwei.qunguanjia.web.monitor.controller;

import com.jinwei.qunguanjia.common.annotation.Log;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.BusinessTypeEnum;
import com.jinwei.qunguanjia.common.utils.JsonUtils;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.common.utils.poi.ExcelUtil;
import com.jinwei.qunguanjia.system.dto.SysOperationLogDTO;
import com.jinwei.qunguanjia.system.service.SysOperationLogService;
import com.jinwei.qunguanjia.web.monitor.vo.SysOperationLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 操作日志记录
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/operationLog")
public class SysOperationLogController {

    @Resource
    private SysOperationLogService sysOperationLogService;

    @PreAuthorize("@ss.hasPermission('monitor:operlog:list')")
    @GetMapping("/list")
    public PageDataInfo<SysOperationLogVO> list(SysOperationLogVO sysOperationLogVO) throws IllegalAccessException {
        SysOperationLogDTO sysOperationLogDTO = new SysOperationLogDTO();
        BeanUtils.copyProperties(sysOperationLogVO, sysOperationLogDTO);
        PageUtils.startPage();
        PageDataInfo<SysOperationLogDTO> pageDataInfo = sysOperationLogService.listPage(sysOperationLogDTO);
        PageDataUtil<SysOperationLogDTO, SysOperationLogVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(pageDataInfo, SysOperationLogVO.class);
    }

    @Log(title = "操作日志-导出", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermission('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperationLogVO sysOperationLogVO) throws IllegalAccessException {
        SysOperationLogDTO sysOperationLogDTO = new SysOperationLogDTO();
        BeanUtils.copyProperties(sysOperationLogVO, sysOperationLogDTO);
        ExcelUtil<SysOperationLogVO> util = new ExcelUtil<>(SysOperationLogVO.class);
        util.exportExcel(response, JsonUtils.copyList(sysOperationLogService.listPage(sysOperationLogDTO).getData(), SysOperationLogVO.class), "操作日志");
    }
}
